﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading;

namespace XiamenQudianCoffee.model
{
    public class IOMsgRobot
    {
        private static IOMsgRobot instance;
        private IOMsgRobot() { }

        public static IOMsgRobot Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new IOMsgRobot();
                }
                return instance;
            }
        }
        #region IO通讯
        public bool AllBol = true;
        public bool RobotBol = true;
        public delegate void IOdelegate(string btnName, string tag);
        public IOdelegate iodelegate;
        /// <summary>
        ///  IO的通讯返回数据
        /// </summary>
        private List<byte> buffer = new List<byte>();
        public static bool IsMsgError = true;
        string nostrs = "";
        public Thread All_thr;
        public void QK()
        {
            try
            {
                Thread.Sleep(100);
                IOSerialPortRobot.Instance.SerialSend(RobotIOModel.Instance.LRobot_KSZZ_Down);
                Thread.Sleep(100);
                IOSerialPortRobot.Instance.SerialSend(RobotIOModel.Instance.LRobot_QJB1_Down);
                Thread.Sleep(100);
                IOSerialPortRobot.Instance.SerialSend(RobotIOModel.Instance.LRobot_QJB2_Down);
                Thread.Sleep(100);
                IOSerialPortRobot.Instance.SerialSend(RobotIOModel.Instance.LRobot_QJCJB1_Down);
                Thread.Sleep(100);
                IOSerialPortRobot.Instance.SerialSend(RobotIOModel.Instance.LRobot_QJCJB2_Down);
                Thread.Sleep(100);
                IOSerialPortRobot.Instance.SerialSend(RobotIOModel.Instance.LRobot_QJGTWZ1_Down);
                Thread.Sleep(100);
                IOSerialPortRobot.Instance.SerialSend(RobotIOModel.Instance.LRobot_QJGTWZ2_Down);
                Thread.Sleep(100);
                IOSerialPortRobot.Instance.SerialSend(RobotIOModel.Instance.LRobot_QZBJ1_Down);
                Thread.Sleep(100);
                IOSerialPortRobot.Instance.SerialSend(RobotIOModel.Instance.LRobot_QZBJ2_Down);
                Thread.Sleep(100);
                IOSerialPortRobot.Instance.SerialSend(RobotIOModel.Instance.LRobot_QJKF_Down);
                Thread.Sleep(100);
                IOSerialPortRobot.Instance.SerialSend(RobotIOModel.Instance.LRobot_QDYJ_Down);
                Thread.Sleep(100);
                IOSerialPortRobot.Instance.SerialSend(RobotIOModel.Instance.LRobot_QG1_Down);
                Thread.Sleep(100);
                IOSerialPortRobot.Instance.SerialSend(RobotIOModel.Instance.LRobot_QG2_Down);
                Thread.Sleep(100);
                IOSerialPortRobot.Instance.SerialSend(RobotIOModel.Instance.LRobot_QG3_Down);
                Thread.Sleep(100);
                IOSerialPortRobot.Instance.SerialSend(RobotIOModel.Instance.LRobot_QCC1_Down);
                Thread.Sleep(100);
                IOSerialPortRobot.Instance.SerialSend(RobotIOModel.Instance.LRobot_QCC2_Down);
                Thread.Sleep(100);
                IOSerialPortRobot.Instance.SerialSend(RobotIOModel.Instance.LRobot_QCD1_Down);
                Thread.Sleep(100);
                IOSerialPortRobot.Instance.SerialSend(RobotIOModel.Instance.LRobot_QCD2_Down);
                Thread.Sleep(100);
                IOSerialPortRobot.Instance.SerialSend(RobotIOModel.Instance.LRobot_QCD3_Down);
                Thread.Sleep(100);
                IOSerialPortRobot.Instance.SerialSend(RobotIOModel.Instance.LRobot_QCD4_Down);
                Thread.Sleep(100);
                IOSerialPortRobot.Instance.SerialSend(RobotIOModel.Instance.LRobot_QCD5_Down);
                Thread.Sleep(100);
                IOSerialPortRobot.Instance.SerialSend(RobotIOModel.Instance.LRobot_QCD6_Down);
                Thread.Sleep(100);
                IOSerialPortRobot.Instance.SerialSend(RobotIOModel.Instance.LRobot_QCD7_Down);
                Thread.Sleep(100);
                IOSerialPortRobot.Instance.SerialSend(RobotIOModel.Instance.LRobot_QCD8_Down);
                Thread.Sleep(100);
                IOSerialPortRobot.Instance.SerialSend(RobotIOModel.Instance.LRobot_QCD9_Down);
                Thread.Sleep(100);
                IOSerialPortRobot.Instance.SerialSend(RobotIOModel.Instance.LRobot_QQD1_Down);
                Thread.Sleep(100);
                IOSerialPortRobot.Instance.SerialSend(RobotIOModel.Instance.LRobot_QQD2_Down);
                Thread.Sleep(100);
                IOSerialPortRobot.Instance.SerialSend(RobotIOModel.Instance.LRobot_QQD3_Down);
                Thread.Sleep(100);
                IOSerialPortRobot.Instance.SerialSend(RobotIOModel.Instance.LRobot_QQD4_Down);
                Thread.Sleep(100);
                IOSerialPortRobot.Instance.SerialSend(RobotIOModel.Instance.LRobot_QQD5_Down);
                Thread.Sleep(100);
                IOSerialPortRobot.Instance.SerialSend(RobotIOModel.Instance.LRobot_QQD6_Down);
                Thread.Sleep(100);
                IOSerialPortRobot.Instance.SerialSend(RobotIOModel.Instance.LRobot_QQD7_Down);
                Thread.Sleep(100);
                IOSerialPortRobot.Instance.SerialSend(RobotIOModel.Instance.LRobot_QQD8_Down);
                Thread.Sleep(100);
                IOSerialPortRobot.Instance.SerialSend(RobotIOModel.Instance.LRobot_QQD9_Down);
                Thread.Sleep(100);
                IOSerialPortRobot.Instance.SerialSend(RobotIOModel.Instance.LRobot_QZS_Down);
                Thread.Sleep(100);
                IOSerialPortRobot.Instance.SerialSend(RobotIOModel.Instance.LRobot_KB_Down);
                Thread.Sleep(100);
                IOSerialPortRobot.Instance.SerialSend(RobotIOModel.Instance.LRobot_JBCG_Down);
                Thread.Sleep(100);
                IOSerialPortRobot.Instance.SerialSend(RobotIOModel.Instance.LRobot_GTWC_Down);
                Thread.Sleep(100);
                IOSerialPortRobot.Instance.SerialSend(RobotIOModel.Instance.LRobot_JBWC_Down);
                Thread.Sleep(100);
                IOSerialPortRobot.Instance.SerialSend(RobotIOModel.Instance.LRobot_KFWC_Down);
                Thread.Sleep(100);
                IOSerialPortRobot.Instance.SerialSend(RobotIOModel.Instance.LRobot_YGWC_Down);
                Thread.Sleep(100);
                IOSerialPortRobot.Instance.SerialSend(RobotIOModel.Instance.LRobot_QQD_Down);
                Thread.Sleep(100);
                IOSerialPortRobot.Instance.SerialSend(RobotIOModel.Instance.LRobot_QCD_Down);
                Thread.Sleep(100);
                IOSerialPortRobot.Instance.SerialSend(RobotIOModel.Instance.LRobot_QFQ_Down);
                Thread.Sleep(100);
                IOSerialPortRobot.Instance.SerialSend(RobotIOModel.Instance.LRobot_QBWC_Down);
            }
            catch (Exception)
            {
                return;
            }
        }
        public void IOSerial_Thr()
        {
            try
            {
                var ddd = SerialPort.GetPortNames().ToList();
                if (!SerialPort.GetPortNames().ToList().Any(a => a.ToLower().Contains(IPorPortInfo.Instance.PortNameR)))
                {
                    Thread.Sleep(1000);
                    IOSerial_Thr();
                }
                IOSerialPortRobot.Instance.IceMachineConnect(IPorPortInfo.Instance.PortNameR, 38400, "", 8, 1);//其他串口通讯
                IOSerialPortRobot.Instance.serialPort.DataReceived += SerialPort_DataReceived;
                Robot_Thr();
                //Thread thr = new Thread(MainWindow.MessageShow);
                //thr.IsBackground = true;
                //thr.Start();
            }
            catch (Exception)
            {
                IOSerialPortRobot.Instance.serialPort.DataReceived -= SerialPort_DataReceived;
                Thread.Sleep(100);
                IOSerial_Thr();
            }
        }
        public void Robot_Thr()
        {
            try
            {
                // RobotOperate.Read_CSV(); //读取轨迹文件
                RobotOperate.Robot_Initialisation_double(); //机器人初始化
            }
            catch (Exception)
            {
                Thread.Sleep(100);
                Robot_Thr();
            }
        }
        /// <summary>
        /// PING 通 IP
        /// </summary>
        /// <param name="IP"></param>
        /// <returns></returns>
        public bool PingIP(string IP)
        {
            try
            {
                if (IP.Split('.').Count() < 4) return false;
                Ping pingSender = new Ping();
                PingReply reply = pingSender.Send(IP, 120);//第一个参数为ip地址，第二个参数为ping的时间
                if (reply.Status == IPStatus.Success)
                {
                    return true;
                }
                else
                {
                    //ping不通
                    return false;
                }
            }
            catch (Exception)
            {
                return false;
            }
        }

        public void AllMessages()
        {
            try
            {
                while (true)
                {
                    try
                    {
                        if (Instance.AllBol)
                            // IOSerialPortRobot.Instance.SerialSend(DigitalMsg.Instance.AllMessage);
                            Thread.Sleep(100);
                    }
                    catch (Exception)
                    {
                        Thread.Sleep(100);
                    }

                }
            }
            catch (Exception)
            {
                IOSerialPortRobot.Instance.serialPort.DataReceived -= SerialPort_DataReceived;
                Thread.Sleep(100);
                IOSerial_Thr();
            }
        }
        private void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            try
            {
                byte[] result = new byte[IOSerialPortRobot.Instance.serialPort.BytesToRead];
                IOSerialPortRobot.Instance.serialPort.Read(result, 0, IOSerialPortRobot.Instance.serialPort.BytesToRead);
                buffer.AddRange(result);
                string str = "";// $"{DateTime.Now}：\n";
                                //判断读写模式
                if (buffer.Count > 0x03)
                    str += $"{Encoding.ASCII.GetString(result)}";
                for (int i = 0; i < result.Length; i++)
                {
                    str += $"{result[i].ToString("X2")} ";
                }
                MsgShow(str);
                Thread.Sleep(1);
            }
            catch (Exception)
            {
                return;
            }
        }
        public void DigitalMsgBol2(string str)
        {
            try
            {

                var Bol = str.Split(' ');
                bool bol = false;
                bool isEmail = false;
                //if (WuLiaoCoffeeSel.Instance.BANNER_Sel_Bol != (Bol[4] == "01" ? true : false))
                //{
                //    bol = true;
                //}
                //WuLiaoCoffeeSel.Instance.BANNER_Sel_Bol = Bol[4] == "01" ? true : false;
                //if (WuLiaoCoffeeSel.Instance.OutMealCup_Sel_Bol != (Bol[10] == "01" ? true : false))
                //{
                //    // Console.WriteLine("出餐buyiyang " +( Bol[10] == "01" ? true : false).ToString());
                //    bol = true;
                //}
                //// Console.WriteLine("出餐 " + (Bol[10] == "01" ? true : false).ToString());
                //WuLiaoCoffeeSel.Instance.OutMealCup_Sel_Bol = Bol[10] == "01" ? true : false;
                //if (WuLiaoCoffeeSel.Instance.PushRodUP_Sel_Bol != (Bol[6] == "01" ? true : false))
                //{
                //    bol = true;
                //}
                //WuLiaoCoffeeSel.Instance.PushRodUP_Sel_Bol = Bol[6] == "01" ? true : false;
                ////  Console.WriteLine("chucanshangdaowei" + WuLiaoCoffeeSel.Instance.PushRodUP_Sel_Bol);
                //if (WuLiaoCoffeeSel.Instance.PushRodDown_Sel_Bol != (Bol[8] == "01" ? true : false))
                //{
                //    bol = true;
                //}
                //WuLiaoCoffeeSel.Instance.PushRodDown_Sel_Bol = Bol[8] == "01" ? true : false;
                //if (WuLiaoCoffeeSel.Instance.TakeCup_Sel_Bol != (Bol[12] == "01" ? true : false))
                //{
                //    bol = true;
                //}
                //WuLiaoCoffeeSel.Instance.TakeCup_Sel_Bol = Bol[12] == "01" ? true : false;
                //if (WuLiaoCoffeeSel.Instance.LBQFK1_Bol != (Bol[14] == "01" ? true : false))
                //{
                //    bol = true;
                //}
                //WuLiaoCoffeeSel.Instance.LBQFK1_Bol = Bol[14] == "01" ? true : false;
                //if (WuLiaoCoffeeSel.Instance.LBQFK2_Bol != (Bol[16] == "01" ? true : false))
                //{
                //    bol = true;
                //}
                //WuLiaoCoffeeSel.Instance.LBQFK2_Bol = Bol[16] == "01" ? true : false;
                //if (WuLiaoCoffeeSel.Instance.CupFeed1_Sel_Bol != (Bol[22] == "01" ? true : false))
                //{
                //    if (WuLiaoCoffeeSel.Instance.CupFeed1_Sel_Bol)
                //        isEmail = true;
                //    bol = true;
                //}
                //WuLiaoCoffeeSel.Instance.CupFeed1_Sel_Bol = Bol[22] == "01" ? true : false;
                //if (WuLiaoCoffeeSel.Instance.CupFeed2_Sel_Bol != (Bol[24] == "01" ? true : false))
                //{
                //    if (WuLiaoCoffeeSel.Instance.CupFeed2_Sel_Bol)
                //        isEmail = true;
                //    bol = true;
                //}
                //WuLiaoCoffeeSel.Instance.CupFeed2_Sel_Bol = Bol[24] == "01" ? true : false;
                //if (WuLiaoCoffeeSel.Instance.Milk_Sel_Bol != (Bol[18] == "01" ? true : false))
                //{
                //    if (WuLiaoCoffeeSel.Instance.Milk_Sel_Bol)
                //        isEmail = true;
                //    bol = true;
                //}
                //WuLiaoCoffeeSel.Instance.Milk_Sel_Bol = Bol[18] == "01" ? true : false;
                //if (WuLiaoCoffeeSel.Instance.Water_Sel_Bol != (Bol[20] == "01" ? true : false))
                //{
                //    bol = true;
                //}
                //WuLiaoCoffeeSel.Instance.Water_Sel_Bol = Bol[20] == "01" ? true : false;
                //if (WuLiaoCoffeeSel.Instance.GT1_Sel_Bol != (Bol[26] == "01" ? true : false))
                //{
                //    if (WuLiaoCoffeeSel.Instance.GT1_Sel_Bol)
                //        isEmail = true;
                //    bol = true;
                //}
                //WuLiaoCoffeeSel.Instance.GT1_Sel_Bol = Bol[26] == "01" ? true : false;
                //if (WuLiaoCoffeeSel.Instance.GT2_Sel_Bol != (Bol[28] == "01" ? true : false))
                //{
                //    if (WuLiaoCoffeeSel.Instance.GT2_Sel_Bol)
                //        isEmail = true;
                //    bol = true;
                //}
                //WuLiaoCoffeeSel.Instance.GT2_Sel_Bol = Bol[28] == "01" ? true : false;
                //if (WuLiaoCoffeeSel.Instance.GT3_Sel_Bol != (Bol[30] == "01" ? true : false))
                //{
                //    if (WuLiaoCoffeeSel.Instance.GT3_Sel_Bol)
                //        isEmail = true;
                //    bol = true;
                //}
                //WuLiaoCoffeeSel.Instance.GT3_Sel_Bol = Bol[30] == "01" ? true : false;
                //if (WuLiaoCoffeeSel.Instance.YBJ_Milk != (Bol[32] == "01" ? true : false))
                //{
                //    if (WuLiaoCoffeeSel.Instance.YBJ_Milk)
                //        isEmail = true;
                //}
                //WuLiaoCoffeeSel.Instance.YBJ_Milk = Bol[32] == "01" ? true : false;
                //if (WuLiaoCoffeeSel.Instance.YBJ_Water != (Bol[34] == "01" ? true : false))
                //{
                //    if (WuLiaoCoffeeSel.Instance.YBJ_Water)
                //        isEmail = true;
                //}
                //WuLiaoCoffeeSel.Instance.YBJ_Water = Bol[34] == "01" ? true : false;
                //if (WuLiaoCoffeeSel.Instance.YBJ_Water != (Bol[34] == "01" ? true : false))
                //{
                //    if (WuLiaoCoffeeSel.Instance.YBJ_Water)
                //        isEmail = true;
                //}
                //WuLiaoCoffeeSel.Instance.YBJ_Water = Bol[34] == "01" ? true : false;
                //if (WuLiaoCoffeeSel.Instance.ZBJ_Water != (Bol[36] == "01" ? true : false))
                //{
                //    if (WuLiaoCoffeeSel.Instance.ZBJ_Water)
                //        isEmail = true;
                //    bol = true;
                //}
                //WuLiaoCoffeeSel.Instance.ZBJ_Water = Bol[36] == "01" ? true : false;
                //if (WuLiaoCoffeeSel.Instance.ZBJYBJ_Water != (Bol[38] == "01" ? true : false))
                //{
                //    if (WuLiaoCoffeeSel.Instance.ZBJYBJ_Water)
                //        isEmail = true;
                //}
                //WuLiaoCoffeeSel.Instance.ZBJYBJ_Water = Bol[38] == "01" ? true : false;
                // WuLiaoCoffeeSel.Instance.YBJ_Ice = Bol[36] == "01" ? true : false;
                if (isEmail)
                {
                    TestDelegateInvoke testDelegate = new TestDelegateInvoke(DelegateInvokeMethod);
                    testDelegate.BeginInvoke(null, null);
                }
                if (bol)
                {
                    ClientList clientList = new ClientList();
                    clientList.MsgBol = WuLiaoCoffeeSel.Instance;
                    clientList.code = 2;
                    SocketClients.Instance.client.Send(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(clientList)));
                }
            }
            catch (Exception)
            {
                return;
            }
        }
        public delegate void TestDelegateInvoke();

        private void DelegateInvokeMethod()
        {
            try
            {
                EmailShow emailShow = new EmailShow();
                emailShow.content = MainWindow.EmailError();
                var saves1 = JsonConvert.SerializeObject(emailShow);
                Console.WriteLine(emailShow.content);
                var dd1 = HttpMsg.Instance.sendEmailMsg(saves1);
            }
            catch (Exception)
            {
                return;
            }
        }
        public void MsgShow(string str)
        {
            try
            {
                var nostr = str.Split(' ');
                nostrs += nostr[0].Substring(nostr[0].Length - 2, 2) + " ";
                for (int i = 1; i < nostr.Count() - 1; i++)
                {
                    nostrs += nostr[i] + " ";
                }
                if (nostrs.Substring(0, 5) == "04 02")
                {
                    Console.WriteLine(nostrs);
                    var robstr = nostrs.Split(' ');
                    RobotBol = robstr[3] == "01" ? true : false;
                    Console.WriteLine(robstr[3] == "01" ? true : false);
                }
                nostrs = "";
            }
            catch (Exception)
            {
                return;
            }
        }
        public class EmailShow
        {
            public string title { get; set; } = "Shortage warning";
            public int deviceId { get; set; } = IPorPortInfo.Instance.deviceID;
            public string content { get; set; } = "";
            public string fromname { get; set; } = "Coffee Shop";
        }
        #endregion
    }
}
